package com.yami.shop.groupbuy.common.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.SkuLang;
import com.yami.shop.groupbuy.common.api.dto.ApiGroupSkuDto;
import com.yami.shop.groupbuy.common.dao.GroupSkuMapper;
import com.yami.shop.groupbuy.common.model.GroupSku;
import com.yami.shop.groupbuy.common.service.GroupSkuService;
import com.yami.shop.manager.impl.LangManager;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 拼团活动商品规格
 *
 * @author LGH
 * @date 2019-08-27 17:55:57
 */
@Service
@AllArgsConstructor
public class GroupSkuServiceImpl extends ServiceImpl<GroupSkuMapper, GroupSku> implements GroupSkuService {

    private final GroupSkuMapper groupSkuMapper;
    private final LangManager langManager;

    /**
     * 获取SKU列表数据
     * @param groupActivityId 拼团活动ID
     * @return SKU列表
     */
    @Override
    public List<ApiGroupSkuDto> getApiByGroupActivityIdAndProdId(Long groupActivityId) {
        List<ApiGroupSkuDto> groupSkuList = groupSkuMapper.getApiByGroupActivityIdAndProdId(groupActivityId);
        if (CollUtil.isEmpty(groupSkuList)) {
            return groupSkuList;
        }
        List<Long> ids = groupSkuList.stream().filter(item -> Objects.nonNull(item.getSkuId())).map(ApiGroupSkuDto::getSkuId).collect(Collectors.toList());
        Map<Long, SkuLang> skuLangMap = langManager.getSkuLangMap(ids);
        for (ApiGroupSkuDto item : groupSkuList) {
            SkuLang skuLang = skuLangMap.get(item.getSkuId());
            if (Objects.nonNull(skuLang)) {
                item.setSkuName(skuLang.getSkuName());
                item.setProperties(skuLang.getProperties());
            }
        }
        return groupSkuList;
    }

}
